package com.bringway.p2p.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.P2PZqfdService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*
 * 
 * @ClassName: P2PZqfdController
 * @Description: 债权分档的控制器
 * @author: 邵jing
 * @date: 2016年2月19日 下午5:28:02
 */
@Controller
@RequestMapping("/zqfd")
public class P2PZqfdController extends JsonController {
	@Autowired
	private P2PZqfdService p2pZqfdService;

	/**
	 * @方法名称: queryZqfdDetails
	 * @方法描述: 查询债权分档的详细信息
	 * @时间:2016年2月19日下午5:39:37
	 * @author:邵jing
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZqfdDetails")
	public ModelAndView queryZqfdDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String DETAILS_NAME = RequestUtils.getStringParameter(request, "DETAILS_NAME");
		String CREATE_DATE_START = RequestUtils.getStringParameter(request, "CREATE_DATE_START");
		String CREATE_DATE_END = RequestUtils.getStringParameter(request, "CREATE_DATE_END");
		String CREATE_USER_ID = RequestUtils.getStringParameter(request, "CREATE_USER_ID");
		String DETAILS_NHL_START = RequestUtils.getStringParameter(request, "DETAILS_NHL_START");
		String DETAILS_NHL_END = RequestUtils.getStringParameter(request, "DETAILS_NHL_END");
		String start = RequestUtils.getStringParameter(request, "start");
		String limit = RequestUtils.getStringParameter(request, "limit");
		JSONObject parms = new JSONObject();
		parms.put("DETAILS_NAME", DETAILS_NAME);
		parms.put("CREATE_DATE_START", CREATE_DATE_START);
		parms.put("CREATE_DATE_END", CREATE_DATE_END);
		parms.put("CREATE_USER_ID", CREATE_USER_ID);
		parms.put("DETAILS_NHL_START", DETAILS_NHL_START);
		parms.put("DETAILS_NHL_END", DETAILS_NHL_END);
		parms.put("start", start);
		parms.put("limit", limit);
		List list = p2pZqfdService.queryZqfdDetails(parms);
		JSONObject result = new JSONObject();
		result.put("data", list);
		int total = p2pZqfdService.queryZqfdDetailsItem(parms);
		result.put("total", total);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @方法名称: queryZqfd
	 * @方法描述: 查询债权分档
	 * @时间:2016年2月20日下午3:46:01
	 * @author:邵jing
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZqfd")
	public ModelAndView queryZqfd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String CREATE_DATE_START = RequestUtils.getStringParameter(request, "CREATE_DATE_START");
		String CREATE_DATE_END = RequestUtils.getStringParameter(request, "CREATE_DATE_END");
		String FD_NAME = RequestUtils.getStringParameter(request, "FD_NAME");
		String CREATE_USER_ID = RequestUtils.getStringParameter(request, "CREATE_USER_ID");
		String FD_SEQ = RequestUtils.getStringParameter(request, "FD_SEQ");
		int start = RequestUtils.getIntParameter(request, "start", 0);
		int limit = RequestUtils.getIntParameter(request, "limit", 0);
		JSONObject params = new JSONObject();
		params.put("CREATE_DATE_START", CREATE_DATE_START);
		params.put("CREATE_DATE_END", CREATE_DATE_END);
		params.put("FD_NAME", FD_NAME);
		params.put("CREATE_USER_ID", CREATE_USER_ID);
		params.put("FD_SEQ", FD_SEQ);
		params.put("start", start);
		params.put("limit", limit);

		JSONObject result = new JSONObject();
		List list = p2pZqfdService.queryZqfd(params);
		int total = p2pZqfdService.queryZqfdItems(params);
		result.put("data", list);
		result.put("total", total);
		serialize(request, response, result);
		return null;
	}

	/**
	 * 
	 * @方法名称: addZqfdDetails
	 * @方法描述: 新增债权分档明细
	 * @时间:2016年2月22日下午1:41:54
	 * @author:邵jing
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addZqfdDetails")
	public ModelAndView addZqfdDetails(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String DETAILS_NAME = RequestUtils.getStringParameter(request, "DETAILS_NAME");
		String DETAILS_NHL = RequestUtils.getStringParameter(request, "DETAILS_NHL");
		String DETAILS_MIN_JE = RequestUtils.getStringParameter(request, "DETAILS_MIN_JE");
		String DETAILS_MAX_JE = RequestUtils.getStringParameter(request, "DETAILS_MAX_JE");
		String CREATE_USER_ID = RequestUtils.getStringParameter(request, "CREATE_USER_ID");
		String CREATE_DATE = RequestUtils.getStringParameter(request, "CREATE_DATE");
		String DETAILS_CONTENT = RequestUtils.getStringParameter(request, "DETAILS_CONTENT");

		JSONObject params = new JSONObject();
		params.put("DETAILS_NAME", DETAILS_NAME);
		params.put("DETAILS_NHL", DETAILS_NHL);
		params.put("DETAILS_MIN_JE", DETAILS_MIN_JE);
		params.put("DETAILS_MAX_JE", DETAILS_MAX_JE);
		params.put("CREATE_USER_ID", CREATE_USER_ID);
		params.put("CREATE_DATE", CREATE_DATE);
		params.put("DETAILS_CONTENT", DETAILS_CONTENT);
		boolean bool = p2pZqfdService.addZqfdDetails(params);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isAdd", bool);
		serialize(request, response, result);
		return null;
	}

	/**
	 * 
	 * @方法名称: checkJE
	 * @方法描述: 检测金额区间是否连续
	 * @时间:2016年2月23日下午1:37:50
	 * @author:邵jing
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/checkJE")
	public ModelAndView checkJE(HttpServletRequest request, HttpServletResponse response) throws Exception {
		boolean bool = true;
		String detailsId = RequestUtils.getStringParameter(request, "detailsId");
		JSONObject params = new JSONObject();
		params.put("ids", detailsId);
		List<JSONObject> list = p2pZqfdService.checkJE(params);
		for (int i = 0; i < list.size() - 1; i++) {
			double maxJe = list.get(i).optDouble("DETAILS_MAX_JE");
			double minJe = list.get(i + 1).optDouble("DETAILS_MIN_JE");
			if (maxJe != minJe) {
				bool = false;
			}
		}
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isContinuous", bool);
		serialize(request, response, result);
		return null;
	}

	@RequestMapping("/addZqfd")
	public ModelAndView addZqfd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String FD_NAME = RequestUtils.getStringParameter(request, "FD_NAME");
		String FD_SEQ = RequestUtils.getStringParameter(request,"FD_SEQ");
		String FD_CONTENT =RequestUtils.getStringParameter(request,"FD_CONTENT");
		String CREATE_USER_ID = RequestUtils.getStringParameter(request,"CREATE_USER_ID");
		String CREATE_DATE = RequestUtils.getStringParameter(request,"CREATE_DATE");
		String detailsId = RequestUtils.getStringParameter(request,"detailsId");
		
		JSONObject params = new JSONObject();
		params.put("FD_NAME", FD_NAME);
		params.put("FD_SEQ", FD_SEQ);
		params.put("FD_CONTENT", FD_CONTENT);
		params.put("CREATE_USER_ID", CREATE_USER_ID);
		params.put("CREATE_DATE", CREATE_DATE);
		params.put("detailsId",detailsId);
		boolean bool = p2pZqfdService.addZqfd(params);
		
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isAdd",bool);
		serialize(request, response, result);
		return null;
	}
}
